using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameEngine
{
    public enum ThreeState
    {
        BEGIN,
        RUN,
        END,
    }

    public class ThreeStateObject : DrawableGameComponent
    {
        protected double _progressTime;
        public double BeginTime;
        public double RunTime;
        public double EndTime;

        protected ThreeState _currentState;
        public ThreeState CurrentState
        {
            get
            {
                return _currentState;
            }
            set
            {
                _currentState = value;
            }
        }

        public ThreeStateObject(Game game) : base(game)
        {
            Initialize();
        }

        public override void Initialize()
        {
            Enabled = false;
            Visible = false;

            BeginTime = 0;
            RunTime = 0;
            EndTime = 0;
            _progressTime = 0;

            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        public override void Update(GameTime gameTime)
        {
            switch (_currentState)
            {
                case ThreeState.BEGIN:
                    _progressTime += gameTime.ElapsedGameTime.TotalSeconds;
                    if (_progressTime > BeginTime)
                    {
                        _progressTime = 0;
                        _currentState = ThreeState.RUN;
                        EndBeginAnimation();
                    }
                    else
                    {
                        float ratio = (float)(_progressTime / BeginTime);
                        BeginAnimation(ratio);
                    }
                    break;
                case ThreeState.RUN:
                    if (RunTime > 0)
                    {
                        _progressTime += gameTime.ElapsedGameTime.TotalSeconds;
                        if (_progressTime > RunTime)
                        {
                            _progressTime = 0;
                            _currentState = ThreeState.END;
                            EndRunAnimation();
                        }
                        else
                        {
                            float ratio = (float)(_progressTime / RunTime);
                            RunAnitation(ratio);
                        }
                    }
                    else
                    {
                        UnknowTimeRunAnimation(gameTime);
                    }
                    break;
                case ThreeState.END:
                    _progressTime += gameTime.ElapsedGameTime.TotalSeconds;
                    if (_progressTime > EndTime)
                    {
                        _progressTime = 0;
                        Release();
                    }
                    else
                    {
                        float ratio = (float)(_progressTime / EndTime);
                        EndAnimation(ratio);
                    }
                    break;
            }
            base.Update(gameTime);
        }

        public virtual void BeginAnimation(float ratio)
        {

        }

        public virtual void EndBeginAnimation()
        {
        }

        public virtual void RunAnitation(float ratio)
        {

        }

        public virtual void EndRunAnimation()
        {

        }

        public virtual void UnknowTimeRunAnimation(GameTime gameTime)
        {

        }

        public virtual void EndAnimation(float ratio)
        {

        }

        public virtual void Active()
        {
            Enabled = true;
            Visible = true;
            _currentState = ThreeState.BEGIN;
        }

        public virtual void Destroy()
        {
            _currentState = ThreeState.END;
        }

        public virtual void Release()
        {
            Enabled = false;
            Visible = false;                        
            Dispose(true);
        }
    }
}
